<?php
namespace app\admin\controller;

use think\Db;
use think\facade\Cache;

class Column extends Base
{
	public function column()
	{
        if ($this->request->isPost() && $this->request->isAjax()) {
            $res = [
                'code'  => 0,
                'msg'   => '',
                'count' => 0,
                'data'  => column_tree(),
            ];

            return json($res);
        }

        $this->assign('column', column_list('id,pid,name'));

		return $this->fetch();
	}

    public function columnSave()
    {
        if ($this->request->isPost() && $this->request->isAjax()) {
            $id = $this->request->param('id/d', 0);
            $data = [
                'pid'           =>  $this->request->param('pid/d', 0),
                'type'          =>  $this->request->param('type/d', 0),
                'url'           =>  trim($this->request->param('url/s', '')),
                'name'          =>  trim($this->request->param('name/s', '')),
                'pic'           =>  trim($this->request->param('pic/s', '')),
                'sort'          =>  $this->request->param('sort/d', 0),
                'menu'          =>  $this->request->param('menu/d', 0),
                'title'         =>  $this->request->param('title/s', ''),
                'keywords'      =>  $this->request->param('keywords/s', ''),
                'description'   =>  $this->request->param('description/s', ''),
                'tpl_column'    =>  trim($this->request->param('tpl_column/s', '')) ?: 'column',
                'tpl_content'   =>  trim($this->request->param('tpl_content/s', '')) ?: 'content',
            ];

            // 添加编辑状态检测
            if ($id) {
                if (in_array($data['pid'], column_childrens_id($id))) {
                    $this->result('', 1, '不能移动到本栏目或子栏目下');
                }

                if (!Db::table('column')->where('id', $id)->update($data)) {
                    $this->result('', 1, '编辑栏目失败');
                }
            } else {
                if (!Db::table('column')->insert($data)) {
                    $this->result('', 1, '添加栏目失败');
                }
            }

            // 更新栏目缓存
            Cache::set('column', Db::table('column')->column('*', 'id'));

            $this->result('', 0, '保存成功');
        }
    }

    public function columnDel() 
    {
        if ($this->request->isPost() && $this->request->isAjax()) {
            $id = $this->request->param('id/d', 0);

            if (Db::table('column')->where('pid', $id)->count()) {
                $this->result('', 1, '该栏目下有子栏目，不能删除');
            }

            if (Db::table('content')->where('column_id', $id)->count()) {
                $this->result('', 1, '该栏目下有内容，不能删除');
            }

            if (!Db::table('column')->delete($id)) {
                $this->result('', 1, '删除失败');
            }

            // 清除单页内容
            Db::table('content_data')->where('column_id', $id)->delete();

            // 更新栏目缓存
            Cache::set('column', Db::table('column')->column('*', 'id'));

            $this->result('', 0, '删除成功');
        }
    }

    public function columnSort()
    {
        if ($this->request->isPost() && $this->request->isAjax()) {
            if (!Db::table('column')->where('id', $this->request->param('id/d', 0))->setField('sort', $this->request->param('sort/d', 0))) {
                $this->result('', 1, '排序失败');
            }

            $this->result('', 0, '排序成功');
        } 
    }

    public function columnMenu() 
    {
        if ($this->request->isPost() && $this->request->isAjax()) {
            if (!Db::table('column')->where('id', $this->request->param('id/d', 0))->setField('menu', $this->request->param('menu/d', 0))) {
                $this->result('', 1, '设置失败');
            }

            $this->result('', 0, '设置成功');
        }
    }

    public function updateCache()
    {
        if (!Cache::set('column', Db::table('column')->column('*', 'id'))) {
            $this->result('', 1, '更新栏目缓存失败');
        }

        $this->result('', 0, '更新栏目缓存成功');
    }
}
